package com.argticling.service;

import com.argticling.dao.interf.ReaderDAO;
import com.argticling.dao.interf.UserDAO;
import com.argticling.model.Article;
import com.argticling.model.User;
import com.argticling.model.chanel.Contact;
import com.argticling.model.privilege.Privilege;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

public class UserService {
	@Autowired
    private UserDAO userDao;
    private ContactService contactService;
    private PrivilegeService privilegeService;
    
	@Autowired
    private ReaderDAO readerDao;

    public void delete(String id) {
    	userDao.deleteUser(id);
    }

    public void persist(User user) {
        userDao.persist(user);
    }
    
    public User getUser(String id){
    	return userDao.getUserByName(id);
    }
    
    public String encriptPass(String pass){
    	return userDao.md5convert(pass);
    }

    public void addContact(Contact contact, String userId) {
    	User user = userDao.getUserById(userId);
//        user.addContact(contact);//todo ver de fixear este issue
        userDao.persist(user);
        //todo ver si existe la necesidad de hacer persist del contacto
    }

    public void addPrivilege(Privilege privilege, String userId) {
    	User user = userDao.getUserById(userId);
    	//user.addPrivilege(privilege);
        userDao.persist(user);
        //todo ver si existe la necesidad de hacer persist del privilegio
    }

    public List<Article> getAllArticlesOfUser(String userId){
        return userDao.getAllArticlesofUser(userId);
    }

    public List<User> getUsers() {
        //todo completar
        return null;
    }

    public void removeContact(String contactId, String id) {
    	Contact contact = contactService.getContactById(contactId);
    	contactService.delete(contact);
        //todo ver si se saca el parametro User
    }

    public void removePrivilege(String privilegeId, String id){
    	Privilege privilege = privilegeService.getPrivilegeById(privilegeId);
        privilegeService.delete(privilege);
    }

    public UserDAO getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDAO userDao) {
        this.userDao = userDao;
    }

    public void setContactService(ContactService contactService) {
        this.contactService = contactService;
    }

    public void setPrivilegeService(PrivilegeService privilegeService) {
        this.privilegeService = privilegeService;
    }

	public boolean userExist(String username) {
		return userDao.checkIfUserExists(username);
	}

	public List<String> getSubscriptors(User user) {
		List<String> readers = readerDao.getReaders(user.getUsername());
		return readers;
	}

}
